//
//  Fraction.m
//  02 - ClassAndObject
//
//  Created by 韩旭 on 16/7/5.
//  Copyright © 2016年 HX. All rights reserved.
//

#import "Fraction.h"

@implementation Fraction
- (instancetype)initWithDeno:(NSInteger)deno Member:(NSInteger)member {
    self = [super init];
    if (self) {
        _denominator = deno;
        _numerator = member;
    }
    return self;
}
+ (instancetype)fractionWithDeno:(NSInteger)deno Member:(NSInteger)member {
    return [[[Fraction alloc] initWithDeno:deno Member:member] autorelease];
}
- (Fraction *)addWithFraction:(Fraction *)fraction {
    NSInteger newDeno = self.denominator * fraction.denominator;
    NSInteger newMember = self.denominator * fraction.numerator + self.numerator * fraction.denominator;
    Fraction *addResule = [Fraction fractionWithDeno:newDeno Member:newMember];
    [addResule readResult];
    [addResule outPut];
    return addResule;
}
- (Fraction *)minWithFraction:(Fraction *)fraction {
    NSInteger newDeno = self.denominator * fraction.denominator;
    NSInteger newMember = self.denominator * fraction.numerator - self.numerator * self.denominator;
    Fraction *minResult = [Fraction fractionWithDeno:newDeno Member:newMember];
    [minResult readResult];
    [minResult outPut];
    return minResult;
}
- (Fraction *)subWithFraction:(Fraction *)fraction {
    NSInteger newDeno = self.denominator * fraction.denominator;
    NSInteger newMember = self.numerator * fraction.numerator;
    Fraction *subResult = [Fraction fractionWithDeno:newDeno Member:newMember];
    [subResult readResult];
    [subResult outPut];
    return subResult;
}
- (Fraction *)remWithFraction:(Fraction *)fraction {
    NSInteger newDeno = self.denominator * fraction.numerator;
    NSInteger newMember = self.numerator * fraction.denominator;
    Fraction *remResult = [Fraction fractionWithDeno:newDeno Member:newMember];
    [remResult readResult];
    [remResult outPut];
    return remResult;
}
- (void)readResult {
    //    求最大公约数
    NSInteger max = [self maxGreatWithNum1:labs(self.numerator) Num2:self.denominator];
    self.denominator = self.denominator / max;
    self.numerator = self.numerator / max;
}
- (NSInteger)maxGreatWithNum1:(NSInteger)num1 Num2:(NSInteger)num2 {
    NSInteger min = num1 < num2 ? num1 : num2;
    NSInteger maxPact = 1;
    for (NSInteger i = min ; i > 0; i--) {
        if (num2 % i == 0 && num1 % i == 0) {
            maxPact = i;
            break;
        }
    }
    return maxPact;
}
- (void)outPut {
    if (self.denominator == self.numerator && self.denominator != 0) {
        NSLog(@"结果为1");
    } else if (self.denominator + self.numerator == 0) {
        NSLog(@"结果为-1");
    } else if (self.denominator == 0) {
        NSLog(@"结果为error");
    } else {
        NSLog(@"%ld  %ld", self.denominator, self.numerator);
    }
}
//比较大小
- (void)compareWithFraction:(Fraction *)fraction {
    if (self.denominator * fraction.numerator - self.numerator * fraction.denominator < 0) {
        NSLog(@" frc1 大于 frc2");
    } else if (self.denominator * fraction.numerator == self.numerator * fraction.denominator) {
        NSLog(@"frc1 等于 frc2");
    } else {
        NSLog(@"frc1 小于 frc2");
    }
}
-(NSString *)description{
    return [NSString stringWithFormat:@"<%ld - %ld>", self.numerator, self.denominator];
}
@end
